﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Drawing.Printing;
using WMS_BLL;
using WMS_DATABASE_MYSQL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_DATABASE_MYSQL.MappingClass.SystemClass;
using WMS_DATABASE_MYSQL.ModalClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WMS_WEBAPI.Controllers.Table
{
    /// <summary>
    /// 组合查询资产的所有状态，包括是否在库，最近一次的出库，最近一次的入库，是否报废等
    /// </summary>
    [ApiController]
    [Route("[controller]/[action]")]
    public class ManageUserController : ControllerBase
    {
        private readonly ILogger<ManageUserController> _logger;
        private readonly string controllerName;
        private readonly IMemoryCache _memoryCache;//内存缓存
        private readonly ISysCurrentUserIBLL _sysCurrentUserIBLL;
        private readonly ISysAuthorityIBLL  _sysAuthorityIBLL;
        public ManageUserController(ILogger<ManageUserController> logger, ISysCurrentUserIBLL sysCurrentUserIBLL,  IMemoryCache memoryCache, ISysAuthorityIBLL sysAuthorityIBLL)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _sysCurrentUserIBLL = sysCurrentUserIBLL;
            _memoryCache = memoryCache;
            _sysAuthorityIBLL = sysAuthorityIBLL;
        }
        //查询所有用户信息
        [Authorize(Roles = "superadmin")]
        [HttpGet]
        public async Task<IActionResult> GetUsersAsync(int current, int pageSize, int? authoritykey, string? loginname, string? realname,string? access)
        {
            var data = await _sysCurrentUserIBLL.ToWherePageListAsync(current, pageSize, a => (authoritykey == null || a.authoritykey == authoritykey) && (loginname == null || a.loginname == loginname)
            && (realname == null || a.realname == realname) && (access == null || a.access == access));
            var datacount = data.Count();
            return Ok(new { data, total = datacount, suceess = true });

        }
        [HttpDelete]
        public async Task<IActionResult> DelUsersAsync([FromBody] List<int> key)
        {
            var t = await _sysCurrentUserIBLL.DeleteRangeAsync(a => key.Contains(a.key));
            if (t > 0)
            {
                var d = await _sysAuthorityIBLL.DeleteRangeAsync(a => key.Contains(a.key));
                if (d > 0) {
                    return Ok("用户删除成功");
                }
            }
            return BadRequest();
        }

        [HttpPut]
        public async Task<IActionResult> EditUserAsync([FromBody] wms_syscurrentuser data)
        {
            var t = await _sysCurrentUserIBLL.EditAsync(data);
            if (t)
            {
                return Ok("用户修改成功");
            }
            return BadRequest();
        }
    }
}
